/**
 * Created by Administrator on 2017/5/5 0005.
 */


function setCookie(key,arr,num){

    var arrStr=JSON.stringify(arr);

    var d=new Date();
    d.setDate(d.getDate()+num);
    document.cookie=encodeURIComponent(key)+"="+encodeURIComponent(arrStr)+";expires="+d+";";
}

function getCookie(){
    var cookie=decodeURIComponent(document.cookie);

    var cookieArr=[];

    if(cookie.indexOf(';')!=-1){
        var arr=cookie.split(';');

        for(var i=0;i<arr.length;i++){


            if(arr[i].indexOf('=')!=-1){

                var obj={};

                var arr1=arr[i].split('=');
                obj[arr1[0].trim()]=arr1[1];
                cookieArr.push(obj);

            }
        }

    }else{

        if(cookie.indexOf('=')!=-1){

            var obj={};

            var arr1=cookie.split('=');
            obj[arr1[0].trim()]=arr1[1];
            cookieArr.push(obj);

        }
    }

return cookieArr;

}

function getKeyForArr(key){

    var arr=getCookie();

    var myArr=[];//当不满足k==key时，也会返回一个空数组；

    for(var i=0,len=arr.length; i<len; i++){


        for (var k in arr[i]) {
            if(k==key){

                if(arr[i][key]=="undefined"||arr[i][key]==undefined||arr[i][key]==null){
                    myArr=[];
                }else{
                    myArr=JSON.parse(arr[i][key]);
                }

            }
        }
    }

    return myArr;

}

//删除cookie
function deletecookie(key,num){
    var str="";
    if(key){
        str+=encodeURIComponent(key)+"= ;";
    }
    if(num){
        var date=new Date();
        date.setDate(date.getDate()-(parsrInt(num)+1));
        str+="expires="+date;
    }
    else{
        var date=new Date();
        date.setDate(date.getDate()-1000);
        str+="expires="+date;
    }
    document.cookie=str;
}